package com.cgw.game.ui;

import android.graphics.Color;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.view.View;


import com.cgw.game.R;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;

public class RhyBarActivity extends AppCompatActivity {
    BarChart barChart;
    private String[] titles = new String[]{
            "12:00","13:00","14:00","15:00","16:00","17:00",
            "18:00","19:00","20:00","21:00","22:00","23:00",
            "00:00","01:00","02:00","03:00","04:00","05:00",
            "06:00","07:00","08:00","09:00","10:00","11:00","12:00"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rhy_bar);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle(getIntent().getStringExtra("title"));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//返回
            }
        });
        barChart = findViewById(R.id.bar_chart);
        initView();
        initData();
    }



    private void initView() {
        barChart.setTouchEnabled(false);
        barChart.setPinchZoom(true);
        barChart.animateY(2000);
        //取消说明
        barChart.getLegend().setEnabled(false);
        barChart.getDescription().setEnabled(false);
        //竖向轴线设置
        YAxis leftAxis = barChart.getAxisLeft();
        leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
        leftAxis.setTextSize(8f);
        leftAxis.setTextColor(Color.DKGRAY);
        leftAxis.setValueFormatter(new IndexAxisValueFormatter());
        leftAxis.setEnabled(true);
        leftAxis.setDrawAxisLine(false);
        leftAxis.enableGridDashedLine(10f, 10f, 0f);
        leftAxis.setDrawAxisLine(false);
        barChart.getAxisRight().setEnabled(false);
        //横向轴线设置
        XAxis xAxis = barChart.getXAxis();
        xAxis.setLabelCount(5,true);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(12);
        xAxis.setTextColor(Color.DKGRAY);
        xAxis.setEnabled(true);
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMinimum(0f);
        //保证Y轴从0开始，不然会上移一点
        xAxis.setAvoidFirstLastClipping(true);//图表将避免第一个和最后一个标签条目被减掉在图表或屏幕的边缘
        leftAxis.setAxisMinimum(0f);
        xAxis.setAxisMinimum(-0.5f);
        xAxis.setGranularity(1f);
        xAxis = barChart.getXAxis();
        xAxis.setValueFormatter(new IAxisValueFormatter() { // 转换要显示的标签文本，value值默认是int从0开始
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return titles[(int)(value)];
            }
        });
        /*xAxis.setAxisLineColor(Color.TRANSPARENT);
        xAxis.setValueFormatter(new IndexAxisValueFormatter());*/
        //设置为虚线
        xAxis.setDrawAxisLine(true);
    }

    private void initData() {
        //初始化柱形图的数据
        List<BarEntry> barEntries = new ArrayList<>();
        List<BarEntry> barEntries1 = new ArrayList<>();
        List<BarEntry> barEntries2 = new ArrayList<>();
        barEntries.add(new BarEntry(0,0));
        barEntries.add(new BarEntry(1,842));
        barEntries.add(new BarEntry(2,0));
        barEntries.add(new BarEntry(3,87));
        barEntries.add(new BarEntry(4,78));
        barEntries.add(new BarEntry(5,92));
        barEntries.add(new BarEntry(6,92));
        barEntries.add(new BarEntry(7,1649));
        barEntries.add(new BarEntry(8,26));
        barEntries.add(new BarEntry(9,199));
        barEntries.add(new BarEntry(10,3136));
        barEntries.add(new BarEntry(11,0));
        barEntries1.add(new BarEntry(12,1500));
        barEntries1.add(new BarEntry(13,1500));
        barEntries1.add(new BarEntry(14,1500));
        barEntries1.add(new BarEntry(15,1500));
        barEntries1.add(new BarEntry(16,1500));
        barEntries1.add(new BarEntry(17,1500));
        barEntries1.add(new BarEntry(18,1500));
        barEntries1.add(new BarEntry(19,1500));
        barEntries.add(new BarEntry(20,1078));
        barEntries.add(new BarEntry(21,0));
        barEntries.add(new BarEntry(22,2312));
        barEntries.add(new BarEntry(23,0));
        barEntries.add(new BarEntry(24,0));
        barChart.clear();
        BarDataSet barDataSet = new BarDataSet(barEntries, "LAR"); // 新建一组柱形图，"LAR"为本组柱形图的Label
        BarDataSet barDataSet1 = new BarDataSet(barEntries1, "LAR"); // 新建一组柱形图，"LAR"为本组柱形图的Label
        barDataSet.setColor(getResources().getColor(R.color.orange_5)); // 设置柱形图颜色
        barDataSet1.setColor(getResources().getColor(R.color.blue_21)); // 设置柱形图颜色

        //不显示值
        barDataSet.setDrawValues(false);
        barDataSet1.setDrawValues(false);
        barDataSet1.setBarBorderWidth(3f);
        barDataSet1.setBarBorderColor(Color.parseColor("#62A0F3"));
        BarData barData = new BarData();
        barData.addDataSet(barDataSet);// 添加一组柱形图，如果有多组柱形图数据，则可以多次addDataSet来设置
        barData.addDataSet(barDataSet1);
        barChart.getXAxis().setAxisMaximum(barData.getXMax()+0.5f);
        //可以发现，传入的值 mBarWidth不是 px 以像素为单位，而是一个百分百值，默认值为0.85f 相当就是85% ,
        //当设置为1f时相当于没有间隔。
        barData.setBarWidth(0.85f);
        barChart.setData(barData);
    }
}
